package com.hyc.myvideo.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.webkit.WebView;
import android.widget.ProgressBar;

public class CommonWebView extends WebView {
  ProgressBar loadMask;
  Boolean isOpenhttp = true;

  public CommonWebView(Context context, AttributeSet attrs) {
    super(context, attrs);
    getSettings().setJavaScriptEnabled(true);
    requestFocus();
    setWebViewClient(new WebViewClient());
  }

  public Boolean getIsOpenhttp() {
    return isOpenhttp;
  }

  public void setIsOpenhttp(Boolean isOpenhttp) {
    this.isOpenhttp = isOpenhttp;
  }

  public class WebViewClient extends android.webkit.WebViewClient {
    public void onReceivedError(WebView view, int errorCode, String description,
                                String failingUrl) {
      view.stopLoading();
      view.clearView();
      //通知加载自定义404页面
    }

    public boolean shouldOverrideUrlLoading(WebView view, String urlConection) {
      if (isOpenhttp) {
        view.loadUrl(urlConection);
      }
      return true;
    }
  }

  public class WebChromeClient extends android.webkit.WebChromeClient {
    @Override
    public void onProgressChanged(WebView view, int newProgress) {
      if (newProgress == 100) {
        loadMask.setVisibility(GONE);
      } else {
        if (loadMask.getVisibility() == GONE) loadMask.setVisibility(GONE);
        loadMask.setProgress(newProgress);
      }
      super.onProgressChanged(view, newProgress);
    }
  }
}
